package com.liuxin.zhiyuan.shujuzhongxin.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.liuxin.zhiyuan.shujuzhongxin.domain.po.VolunteerRecommendationTempEntity;
import com.liuxin.zhiyuan.shujuzhongxin.service.VolunteerRecommendationTempService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.liuxin.zhiyuan.shujuzhongxin.domain.po.VolunteerRecommendationEntity;
import com.liuxin.zhiyuan.shujuzhongxin.service.VolunteerRecommendationService;
import com.liuxin.common.utils.PageUtils;
import com.liuxin.common.utils.R;



/**
 * 志愿推荐表
 *
 * @author zsl
 * @email 1301966496@qq.com
 * @date 2025-11-22 19:55:50
 */
@RestController
@RequestMapping("shujuzhongxin/volunteerrecommendationredis")
public class VolunteerRecommendationRedisController {
    @Autowired
    private VolunteerRecommendationService volunteerRecommendationService;
    @Autowired
    private VolunteerRecommendationTempService volunteerRecommendationTempService;

    /**
     * 根据学员id查询志愿记录
     */
    @RequestMapping("/getinfobyid")
    //@RequiresPermissions("shujuzhongxin:volunteerrecommendation:list")
    public R getInfoByXueyuanId(@RequestParam Map<String, Object> params){
        Integer id =Integer.parseInt(params.get("id").toString()) ;
        if (id == null) {
            return R.error("id不能为空");
        }
        VolunteerRecommendationEntity result = volunteerRecommendationService.getInfoById(id);
        if (result == null) {
            return R.error("未找到该志愿记录");
        }
        return R.ok().put("data", result);
    }
    /**
     * 保存志愿推荐记录
     */
    @RequestMapping("/save")
    //@RequiresPermissions("shujuzhongxin:volunteerrecommendation:save")
    public R save(@RequestBody VolunteerRecommendationEntity volunteerRecommendation){
        // 检查是否已存在相同xueyuanid的记录
        if (StringUtils.isBlank(String.valueOf(volunteerRecommendation.getId()))) {
            return R.error("id不能为空");
        }
        VolunteerRecommendationEntity VolunteerRecommendationEntity = volunteerRecommendationService.getInfoById(volunteerRecommendation.getId());
        if (VolunteerRecommendationEntity == null) {
            return R.error("未找到该志愿记录");
        }
        // 更新记录
        VolunteerRecommendationEntity.setJieguo(volunteerRecommendation.getJieguo());
        VolunteerRecommendationEntity.setTiaojian(volunteerRecommendation.getTiaojian());
        VolunteerRecommendationEntity.setUpdateTime(new Date());
        volunteerRecommendationService.updateById(VolunteerRecommendationEntity);

        VolunteerRecommendationTempEntity tempRecord = new VolunteerRecommendationTempEntity();
        tempRecord.setVrId(VolunteerRecommendationEntity.getId());
        tempRecord.setXueyuanId(VolunteerRecommendationEntity.getXueyuanId());
        tempRecord.setTiaojian(volunteerRecommendation.getTiaojian());
        tempRecord.setJieguo(volunteerRecommendation.getJieguo());
        tempRecord.setCreateTime(new Date());
        tempRecord.setUpdateTime(new Date());
        volunteerRecommendationTempService.saveOrUpdateByVrId(tempRecord);
        return R.ok();
    }

    /**
     * 修改志愿推荐记录
     */
    //@RequestMapping("/update")
    //@RequiresPermissions("shujuzhongxin:volunteerrecommendation:update")
    public R update(@RequestBody VolunteerRecommendationEntity volunteerRecommendation){
        volunteerRecommendationService.updateById(volunteerRecommendation);
        // 生成志愿填报记录存入lxzy_VolunteerRecommendation_temp表
        VolunteerRecommendationTempEntity tempRecord = new VolunteerRecommendationTempEntity();
        tempRecord.setXueyuanId(volunteerRecommendation.getXueyuanId());
        tempRecord.setTiaojian(volunteerRecommendation.getTiaojian());
        tempRecord.setJieguo(volunteerRecommendation.getJieguo());
        tempRecord.setCreateTime(new Date());
        tempRecord.setUpdateTime(new Date());
        volunteerRecommendationTempService.saveOrUpdateByVrId(tempRecord);
        return R.ok();
    }




    /**
     * 查询所有志愿填报生成列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("shujuzhongxin:volunteerrecommendation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = volunteerRecommendationService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 查询志愿填报生成历史记录列表
     */
    @RequestMapping("/listredis")
    //@RequiresPermissions("shujuzhongxin:volunteerrecommendation:list")
    public R listredis(@RequestParam Map<String, Object> params){
        // 构建查询条件
        String vrId = (String) params.get("vrId");
        if (vrId == null) {
            return R.error("vrId不能为空");
        }
        PageUtils page = volunteerRecommendationTempService.queryPageByVrId(params);
        return R.ok().put("data", page);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("shujuzhongxin:volunteerrecommendation:delete")
    public R delete(@RequestBody Integer[] ids){
		volunteerRecommendationTempService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
